<?php
 namespace  StrategyPattern;

// 1. 定义折扣策略接口：声明统一的折扣计算方法
interface DiscountStrategy
{
    public function calculate(float $price): float;
}

// 2. 定义具体的实现策略类：满减、打折、会员等
class FullReductionStrategy implements DiscountStrategy
{
    public function calculate(float $price): float
    {
        return $price >= 100 ? $price - 20 : $price;
    }
}

class PercentageDiscountStrategy implements DiscountStrategy
{
    public function calculate(float $price): float
    {
        return $price * 0.75;
    }
}

class VipDiscountStrategy implements DiscountStrategy
{
    public function calculate(float $price):  float
    {
        return $price * 0.7;
    }
}

// 定义上下文类，接收策略并执行计算的业务入口
class PriceCalculator
{
    private DiscountStrategy $strategy;

    public function setStrategy(DiscountStrategy $strategy): void
    {
        $this->strategy = $strategy;
    }

    public function calcPrice(float $price): float
    {
        return $this->strategy->calculate($price);
    }
}

// 测试
$originalPrice = 100;
$priceCalculator = new PriceCalculator();
// 满减
$priceCalculator->setStrategy(new FullReductionStrategy());
$price = $priceCalculator->calcPrice($originalPrice);
echo "【满减后单价】：" . $price . "\n";
// 打折
 $priceCalculator->setStrategy(new PercentageDiscountStrategy());
 $price = $priceCalculator->calcPrice($originalPrice);
 echo "【打折后单价】：" . $price . "\n";
// VIP折扣
 $priceCalculator->setStrategy(new VipDiscountStrategy());
 $price = $priceCalculator->calcPrice($originalPrice);
 echo "【会员折扣后单价】：" . $price . "\n";